package com.jielihaofeng.netty.bytebuf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.CompositeByteBuf;

import static com.jielihaofeng.netty.bytebuf.TestByteBuf.log;

/**
 * @author Johnnie Wind
 * @description 测试 compositeByteBuf
 * @date 2021/11/10 14:17
 */
public class TestCompositeByteBuf {
    public static void main(String[] args) {
        // 先创建两个 ByteBuf：buf1 和 buf2
        ByteBuf buf1 = ByteBufAllocator.DEFAULT.buffer(5);
        buf1.writeBytes(new byte[]{1, 2, 3, 4, 5});
        ByteBuf buf2 = ByteBufAllocator.DEFAULT.buffer(5);
        buf2.writeBytes(new byte[]{6, 7, 8, 9, 10});
        log(buf1);
        log(buf2);

        // 创建一个新的 ByteBuf，写入 buf1 和 buf2
//        ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
//        buffer.writeBytes(buf1).writeBytes(buf2);
//        log(buffer);

        // 创建一个 CompositeByteBuf，组合 buf1 和 buf2
        CompositeByteBuf compositeByteBuf = ByteBufAllocator.DEFAULT.compositeBuffer();
        // true表示开启自增写指针，否则组合后写指针是0，没有可读区域了
        compositeByteBuf.addComponents(true, buf1, buf2);
        log(compositeByteBuf);
    }
}
